<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script src="../plugins/nprogress/nprogress.js"></script>
    <link rel="stylesheet" href="../plugins/nprogress/nprogress.css">
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }

        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>账户管理
            <small>菜单管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>账户管理</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="菜单名称" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="findPage2()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate(1)">新增一级菜单</el-button>
                <el-button type="primary" class="butT" @click="handleCreate(2)">新增二级菜单</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column prop="name" label="菜单名称" align="center"></el-table-column>
                <!--<el-table-column prop="level" label="菜单级别" align="center"></el-table-column>-->
                <el-table-column label="菜单级别" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.level == '1' ? '一级菜单' : scope.row.level == '2' ? '二级菜单':''}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="linkUrl" label="访问路径" align="center"></el-table-column>
                <el-table-column prop="path" label="路由路径" align="center"></el-table-column>
                <el-table-column prop="priority" label="优先级" align="center"></el-table-column>
                <el-table-column prop="description" label="描述" align="center"></el-table-column>
                <el-table-column prop="icon" label="图标" align="center"></el-table-column>
                <!--<el-table-column prop="parentMenuId" label="父菜单id" align="center"></el-table-column>-->
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="一级菜单" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="菜单名称">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="优先级">
                                                <el-input v-model="formData.priority"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="菜单图标">
                                                <el-input v-model="formData.icon"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="描述">
                                                <el-input v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>

                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="角色列表" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>菜单ID</th>
                                            <th>菜单名称</th>
                                            <th>描述</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="checkRoleIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.id}}</label></td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.description}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd(1)">确定</el-button>
                    </div>
                </el-dialog>
            </div>


            <div class="add-form">
                <el-dialog title="二级菜单" :visible.sync="dialogFormVisible2">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="父菜单">
                                                <el-select v-model="formData.parentMenuId">
                                                    <el-option
                                                            v-for="items in parentMenuList"
                                                            :label="items.name"
                                                            :value="items.id">
                                                    </el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="菜单名称">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="优先级">
                                                <el-input v-model="formData.priority"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="访问路径">
                                                <el-input v-model="formData.linkUrl"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="描述">
                                                <el-input v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>

                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="角色列表" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>菜单ID</th>
                                            <th>菜单名称</th>
                                            <th>描述</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="checkRoleIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.id}}</label></td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.description}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible2 = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd(2)">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    NProgress.configure({
        easing: 'ease',  // 动画方式
        speed: 500,  // 递增进度条的速度
        showSpinner: false, // 是否显示加载ico
        trickleSpeed: 200, // 自动递增间隔
        minimum: 0.3 // 初始化时的最小百分比
    })
    axios.interceptors.request.use(
        res => {
            NProgress.start();
            return res
        }, error => {
            return Promise.reject(error);
        }
    );
    axios.interceptors.response.use(response => {
        NProgress.done();
        return response;
    }, error => {
        NProgress.done();

    });
    var vue = new Vue({
        el: '#app',
        data: {
            /* autoUpload:true,//自动上传
             imageUrl:null,//模型数据，用于上传图片完成后图片预览*/
            activeName: 'first',//添加/编辑窗口Tab标签名称
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 10,
                total: 100,
                queryString: null,
            },
            dataList: [],//列表数据
            formData: {},//表单数据
            tableData: [],//添加表单窗口中检查组列表数据
            checkRoleIds: [],//添加表单窗口中检查组复选框对应id
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisible2: false,//控制添加窗口显示/隐藏
            parentMenuList: false//父菜单
        },
        created() {
            this.findPage();
        },
        methods: {

            //编辑
            handleEdit(e) {
                //编辑一级菜单
                if (e == 1) {
                    //目前拥有的值
                    // name/priority/icon/discription
                    //缺少的值
                    // path/level
                    this.formData.path = parseInt(this.formData.priority)+1;
                    this.formData.level = e;
                    //把套餐数据(formData)和选择的检查组的id(checkgroupIds)提交到服务器
                    axios.post("/menu/edit.do?checkRoleIds=" + this.checkRoleIds+"&level="+e, this.formData).then(response => {
                        console.log(this.checkRoleIds);
                        this.$message({
                            message: response.data.message,
                            type: response.data.flag ? 'success' : 'error'
                        });
                        //新增成功, 关闭dialog, 查询展示
                        if (response.data.flag) {
                            this.dialogFormVisible = false;
                            this.findPage();
                        }
                    });
                }
                //新增二级菜单
                else {
                    // 目前拥有的值
                    // parentMenuId/name/priority/linkUrl/discription
                    // 缺少
                    // path/level
                    axios.post("/menu/findPathByParentMenuId.do?parentMenuId="+this.formData.parentMenuId).then(res=>{
                        // this.formData.parentPath = res.data.data
                        this.formData.path = "/"+res.data.data.path+"-"+this.formData.priority;
                        this.formData.level = e;
                        //把套餐数据(formData)和选择的检查组的id(checkgroupIds)提交到服务器
                        axios.post("/menu/edit.do?checkRoleIds=" + this.checkRoleIds+"&level="+e, this.formData).then(response => {
                            this.$message({
                                message: response.data.message,
                                type: response.data.flag ? 'success' : 'error'
                            });
                            //新增成功, 关闭dialog, 查询展示
                            if (response.data.flag) {
                                this.dialogFormVisible2 = false;
                                this.findPage();
                            }
                        });
                    }).catch((err)=>{
                        this.$message({
                            message: "请选择父菜单",
                            type:  'error'
                        })
                    })

                }
            },
            //添加
            handleAdd(e) {
                if(this.formData.id!=null){
                    this.handleEdit(e);
                }else{
                    //新增一级菜单
                    if (e == 1) {
                        //目前拥有的值
                        // name/priority/icon/discription
                        //缺少的值
                        // path/level
                        this.formData.path = parseInt(this.formData.priority)+1;
                        this.formData.level = e;
                        //把套餐数据(formData)和选择的检查组的id(checkgroupIds)提交到服务器
                        axios.post("/menu/add.do?checkRoleIds=" + this.checkRoleIds+"&level="+e, this.formData).then(response => {
                            console.log(this.checkRoleIds);
                            this.$message({
                                message: response.data.message,
                                type: response.data.flag ? 'success' : 'error'
                            });
                            //新增成功, 关闭dialog, 查询展示
                            if (response.data.flag) {
                                this.dialogFormVisible = false;
                                this.findPage();
                            }
                        });
                    }
                    //新增二级菜单
                    else {
                        // 目前拥有的值
                        // parentMenuId/name/priority/linkUrl/discription
                        // 缺少
                        // path/level
                        axios.post("/menu/findPathByParentMenuId.do?parentMenuId="+this.formData.parentMenuId).then(res=>{
                            console.log(res);
                            // this.formData.parentPath = res.data.data
                            this.formData.path = "/"+res.data.data.path+"-"+this.formData.priority;
                            this.formData.level = e;
                            //把套餐数据(formData)和选择的检查组的id(checkgroupIds)提交到服务器
                            axios.post("/menu/add.do?checkRoleIds=" + this.checkRoleIds+"&level="+e, this.formData).then(response => {
                                console.log(this.checkRoleIds);
                                this.$message({
                                    message: response.data.message,
                                    type: response.data.flag ? 'success' : 'error'
                                });
                                //新增成功, 关闭dialog, 查询展示
                                if (response.data.flag) {
                                    this.dialogFormVisible2 = false;
                                    this.findPage();
                                }
                            });
                        }).catch((err)=>{
                            this.$message({
                                message: "请选择父菜单",
                                type:  'error'
                            })
                        })

                    }
                }

            },
            findPage2() {
                //从第一页开始查
                this.pagination.currentPage = 1;
                this.findPage();
            },
            //分页查询
            findPage() {
                //1.封装请求参数(currentPage,pageSize,queryString)
                var params = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                //2.使用axios请求服务器,携带请求参数, 获得响应结果,绑定数据({total:199,rows:[]})
                axios.post("/menu/findPage.do", params).then(response => {
                    this.pagination.total = response.data.total;
                    this.dataList = response.data.rows;
                });
            },
            // 重置表单
            resetForm() {
                this.formatData = {};
            },
            // 弹出添加窗口
            handleCreate(e) {
                this.checkRoleIds = [];
                this.formData = {};
                if (e == 2) {
                    axios.get("/menu/findParentMenuList.do").then(response => {
                        if (response.data.flag) {
                            //检查组的数据绑定
                            this.parentMenuList = response.data.data;
                            //弹出dialog
                            this.dialogFormVisible2 = true;
                        }
                    });
                } else {
                    this.dialogFormVisible = true;
                }

                //获得所有
                axios.get("/menu/findAll.do").then(response => {
                    if (response.data.flag) {
                        //检查组的数据绑定
                        this.tableData = response.data.data;
                    }
                });

            },
            // 弹出编辑窗口
            handleUpdate(row) {
                this.formData = row;
                //获得所有
                axios.get("/menu/findAll.do").then(response => {
                    if (response.data.flag) {
                        //检查组的数据绑定
                        this.tableData = response.data.data;
                        axios.post("/menu/findCheckRoleIds.do?id="+row.id).then(res=>{
                            if (res.data.flag) {
                                //检查组的数据绑定
                                this.checkRoleIds = res.data.data;
                            }
                        })
                    }
                });
                // 弹出一级窗口
                if(row.level==1){
                    this.dialogFormVisible = true;
                }else if (row.level==2){
                    axios.get("/menu/findParentMenuList.do").then(response => {
                        if (response.data.flag) {
                            //检查组的数据绑定
                            this.parentMenuList = response.data.data;
                            //弹出dialog
                            this.dialogFormVisible2 = true;
                        }
                    });
                }
            },
            //切换页码
            handleCurrentChange(currentPage) {
                //更新页面
                this.pagination.currentPage = currentPage;
                //再调用findPage()
                this.findPage();
            },
            //删除
            handleDelete(row){
                axios.get("/menu/delete.do?id="+row.id).then(response => {
                    if (response.data.flag) {
                       //删除成功
                        this.$message({
                            message: "删除成功",
                            type:  'success'
                        })
                        this.findPage();
                    }
                });
            }
        },
    })
</script>
</html>
